package com.shaozi.nc.hibservice;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shaozi.data.hib.dao.BaseHibDao;
import com.shaozi.data.hib.service.BaseServiceImpl;
import com.shaozi.lib.throwable.SzException;
import com.shaozi.utils.common.SysUtils;
import com.shaozi.nc.data.hibVO.CrmOpenSeaRuleVO;
import com.shaozi.nc.quartz.crm.pojo.OpenSeeRulePipelinePojo;

@Service(value = "crmOpenSeeRuleService")
public class CrmOpenSeeRuleService extends BaseServiceImpl<CrmOpenSeaRuleVO> {

	private Logger logger = Logger.getLogger(CrmOpenSeeRuleService.class);

	@Autowired
	public CrmOpenSeeRuleService(BaseHibDao<CrmOpenSeaRuleVO> baseHibDao) {
		super(baseHibDao);

	}

	/**
	 * 跟进退回公海
	 * 
	 * @param companyId
	 * @param pipelineIds
	 *            for debug
	 * @return
	 * @throws SzException
	 */
	public List<OpenSeeRulePipelinePojo> noActivityDidBack(String companyId, List<Integer> pipelineIds)
			throws SzException {
		String hql = "select rule.open_sea_id,rule.no_activity_day,sea.pipeline_id from desk_crm_open_sea_rule as rule left join desk_crm_open_sea as sea on sea.id=rule.open_sea_id  WHERE rule.is_no_activity_auto_back >= 1 AND rule.no_activity_day > 0 AND rule.company_id= "
				+ companyId;
		if (pipelineIds != null && !pipelineIds.isEmpty()) {
			hql += " and sea.pipeline_id in (" + SysUtils.combineList(pipelineIds, ",") + ")";
		}
		List objs = getDao().getDaoSupport().rawQuery(hql, -1, -1);
		List<OpenSeeRulePipelinePojo> ret = getOpenSeeRulePipelinePojo(objs, "noActivityDay");
		return ret;
	}

	/**
	 * 
	 * @param companyId
	 * @param pipelineIds
	 *            for debug
	 * @return
	 * @throws SzException
	 */
	// 成单退回公海
	public List<OpenSeeRulePipelinePojo> noOrderDidBack(String companyId, List<Integer> pipelineIds)
			throws SzException {
		String hql = "select rule.open_sea_id,rule.no_order_day,sea.pipeline_id from desk_crm_open_sea_rule rule left join desk_crm_open_sea sea on sea.id=rule.open_sea_id WHERE rule.is_no_order_auto_back >= 1 AND rule.no_order_day > 0 AND rule.company_id= "
				+ companyId;
		if (pipelineIds != null && !pipelineIds.isEmpty()) {
			hql += " and sea.pipeline_id in(" + SysUtils.combineList(pipelineIds, ",") + ")";
		}
		List objs = getDao().getDaoSupport().rawQuery(hql, -1, -1);
		List<OpenSeeRulePipelinePojo> ret = getOpenSeeRulePipelinePojo(objs, "noOrderDay");
		return ret;
	}

	/**
	 * 未跟进退回公海
	 * 
	 * @param companyId
	 * @param pipelineIds
	 *            for debug
	 * @return
	 * @throws SzException
	 */
	public List<OpenSeeRulePipelinePojo> noActivityBack(String companyId, List<Integer> pipelineIds)
			throws SzException {
		String hql = "select rule.open_sea_id,rule.no_activity_day,sea.pipeline_id from desk_crm_open_sea_rule as rule left join desk_crm_open_sea as sea on sea.id=rule.open_sea_id  WHERE rule.is_no_activity_auto_back >= 1 AND rule.no_activity_day > 0 AND rule.company_id= "
				+ companyId;
		if (pipelineIds != null && !pipelineIds.isEmpty()) {
			hql += " and sea.pipeline_id in(" + SysUtils.combineList(pipelineIds, ",") + ")";
		}
		List objs = getDao().getDaoSupport().rawQuery(hql, -1, -1);
		List<OpenSeeRulePipelinePojo> ret = getOpenSeeRulePipelinePojo(objs, "noActivityDay");
		return ret;
	}

	/**
	 * 
	 * @param companyId
	 * @param pipelineIds
	 *            for debug
	 * @return
	 * @throws SzException
	 */
	// SELECT `open_sea_id`,`no_order_day` FROM `desk_crm_open_sea_rule` WHERE
	// `is_no_order_auto_back` >= 1 AND `no_order_day` > 0 AND `company_id` =
	// 10003;
	// 未成单退回公海
	public List<OpenSeeRulePipelinePojo> noOrderBack(String companyId, List<Integer> pipelineIds) throws SzException {
		String hql = "select rule.open_sea_id,rule.no_order_day,sea.pipeline_id from desk_crm_open_sea_rule rule left join desk_crm_open_sea sea on sea.id=rule.open_sea_id WHERE rule.is_no_order_auto_back >= 1 AND rule.no_order_day > 0 AND rule.company_id= "
				+ companyId;
		if (pipelineIds != null && !pipelineIds.isEmpty()) {
			hql += " and sea.pipeline_id in(" + SysUtils.combineList(pipelineIds, ",") + ")";
		}
		List objs = getDao().getDaoSupport().rawQuery(hql, -1, -1);
		List<OpenSeeRulePipelinePojo> ret = getOpenSeeRulePipelinePojo(objs, "noOrderDay");
		return ret;
	}

	@SuppressWarnings("unchecked")
	private List<OpenSeeRulePipelinePojo> getOpenSeeRulePipelinePojo(List objs, String dayField) {
		final List<OpenSeeRulePipelinePojo> ret = new ArrayList<>();
		objs.forEach(obj -> {
			Object[] data = (Object[]) obj;
			OpenSeeRulePipelinePojo pojo = new OpenSeeRulePipelinePojo();
			pojo.setOpenSeaId((Integer) data[0]);
			try {
				BeanUtils.setProperty(pojo, dayField, (Integer) data[1]);
			} catch (Exception e) {
				logger.error(SzException.getStackMessage(e));
			}
			pojo.setPipelineId((Integer) data[2]);
			ret.add(pojo);
		});
		return ret;
	}
}
